global class OpportunityOnAccountDetailExtension implements SObjectPaginatorListener {
 
String selectedStage;
Account acc;
     
    global List<oppwrapper> accounts   {get;set;}
    global SObjectPaginator paginator  {get;set;} 
    global Integer          pageNumber {get;set;}
    private String sortDirection = 'ASC';
    private String sortExp = 'name';
     
List<opportunity> oppList = new List<opportunity>();
 
    public OpportunityOnAccountDetailExtension(ApexPages.StandardController controller) {
        this.acc = (Account)controller.getRecord();
        oppList = null;
        this.accounts = new List<oppwrapper>();
        this.paginator = new SObjectPaginator(
            2,                          //pageSize
            new List<integer>{2,5,10, 25, 50, 100, 200},  //pageSizeIntegerOptions
            this                        //listener
        ); 
        //this.paginator.setRecords(getoppList());
         
    }
     
    public String getselectedStage() {
        return selectedStage;
    }
  
    public void setselectedStage(String selectedStage) {
        system.debug('%%%%%%%%%%%% ' + this.selectedStage);
        this.selectedStage = selectedStage;
        system.debug('%%%%%%%%%%%% ' + this.selectedStage);
    }
     
    public List<selectoption> getItems() {
            List<selectoption> options = new List<selectoption>();
            options.add(new SelectOption('','--SELECT Opportunity Stage--'));
             
            Schema.DescribeFieldResult field = Opportunity.StageName.getDescribe();
  
        for (Schema.PicklistEntry f : field.getPicklistValues())
          options.add(new SelectOption(f.getLabel(),f.getLabel()));
           
            return options;
        }
     
    /*    
    public List<opportunity> getoppList(){
        system.debug('$$$$$$$$$$$$$$$$$$ ' + selectedStage);
            if(selectedStage==null){
                oppList  = null;
            }else{
            oppList  = [select name, stageName, Amount, CloseDate from Opportunity where AccountId =: acc.id and StageName =: String.escapeSingleQuotes(selectedStage)];
            }
             
            return oppList;
                 
    }
    */
     
   public PageReference handleAccountSelected(){
        system.debug('$$$$$$$$$$ in handleAccountSelected()');
        string sortFullExp = sortExpression  + ' ' + sortDirection;
        string query = 'select name, stageName, Amount, CloseDate from Opportunity where AccountId =';
        query = query + ' ' + acc.id + ' ' + ' ';
        query = query + 'and StageName =' + ' '  + String.escapeSingleQuotes(selectedStage) + ' '+ ' order by ' + sortFullExp;
        system.debug('################## ' + query);
        this.paginator.setRecords([select name, stageName, Amount, CloseDate from Opportunity where AccountId =: acc.id and StageName =: String.escapeSingleQuotes(selectedStage)]);
         
        return null;
    }
     
     
    public PageReference test(){
        return null;
    } 
 
 
    global void handlePageChange(List<sobject> newPage){
        this.accounts.clear();
        if(newPage != null && newPage.size() > 0){
            for(Integer i = 0; i < newPage.size(); i++){
                this.accounts.add(
                    new OppWrapper(
                        (Opportunity)newPage.get(i)
                        , i + this.paginator.pageStartPosition + 1
                    )
                );
            }
        }
    }
     
     
    global PageReference skipToPage(){
        this.paginator.skipToPage(pageNumber);
        return null;
    }
     
       public String sortExpression
   {
     get
     {
        return sortExp;
     }
     set
     {
       //if the column is clicked on then switch between Ascending and Descending modes
       if (value == sortExp)
         sortDirection = (sortDirection == 'ASC')? 'DESC' : 'ASC';
       else
         sortDirection = 'ASC';
       sortExp = value;
     }
   }
 
 public String getSortDirection()
 {
    //if not column is selected 
    if (sortExpression == null || sortExpression == '')
      return 'ASC';
    else
     return sortDirection;
 }
 
 public void setSortDirection(String value)
 {  
   sortDirection = value;
 }
  
     
        
    public class OppWrapper{
         
        public Opportunity obj{get;set;}
        public Integer serialNumber{get;set;}
        public Boolean selected{get;set;}
         
        public OppWrapper(Opportunity obj, Integer serialNumber){
            this.obj = obj;
            this.serialNumber = serialNumber;
            system.debug('@@@@@@@@@@@@@@@@ ' + obj.Name);
        }
 
         
    }
     
     
}